const router = require('express').Router();// 分发请求
const parkingRecordModel = require('../../model/parkingRecordModel');// 执行sql语句的模块
const Jwt = require('../../common/token/jwt');//处理token的工具 json web token
// 停车记录管理
// URL: /api/parkingRecord/enter
// 参数： token , vehicle_num, parking_record_enter_time
// 请求方式: POST
router.post('/enter', async (req, res) => {
    let token = req.body.token || req.headers.token;
    let parking_record_enter_time = req.body.parking_record_enter_time;
    let vehicle_num = req.body.vehicle_num;
    let result = new Jwt(token).verifyToken()
    if (result.code == 200) {
        // 记录入场车辆
        await parkingRecordModel.enter(vehicle_num, parking_record_enter_time).then(
            (content) => {
                // 响应数据给前端
                res.send(content)
            },
            (err) => {
                res.send(err)
            }
        )
    }
    else {
        // 不是登录状态
        res.send({
            code: -1,
            msg: '您还没有登录哦。'
        })
    }
})

// 记录离场车辆信息
// URL: /api/parkingRecord/leave
// 参数： token , vehicle_num, parking_record_leave_time
// 请求方式: POST
router.post('/leave', async (req, res) => {
    let token = req.body.token || req.headers.token;
    let parking_record_leave_time = req.body.parking_record_leave_time;
    let vehicle_num = req.body.vehicle_num;
    let result = new Jwt(token).verifyToken()
    if (result.code == 200) {
        // 记录入场车辆
        await parkingRecordModel.leave(vehicle_num, parking_record_leave_time).then(
            (content) => {
                // 响应数据给前端
                // console.log(content)
                res.send(content)
            },
            (err) => {
                res.send(err)
            }
        )
    }
    else {
        // 不是登录状态
        res.send({
            code: -1,
            msg: '您还没有登录哦。'
        })
    }
})

// 所有车辆停车记录
// URL: /api/parkingRecord/list
// 参数：token, page, pageSize
router.get('/list', async (req, res) => {
    let { token, page, pageSize } = req.query;
    let result = new Jwt(token).verifyToken()
    if (result.code == 200) {
        //获取车辆停车记录
        if (token && page && pageSize) {
            await parkingRecordModel.list(page, pageSize).then(
                (content) => {
                    res.send(content)
                },
                (error) => {
                    res.send(error)
                }
            )
        }
        else {
            res.send({
                code: -1,
                msg: '缺少参数。'
            })
        }
    }
    else {
        // 不是登录状态
        res.send({
            code: -1,
            msg: '您还没有登录哦。'
        })
    }
})


// 用户停车记录
// URL: /api/parkingRecord/userlist
// 参数：token, vehicle_num
router.get('/userlist', async (req, res) => {
    let { token, vehicle_num } = req.query;
    let result = new Jwt(token).verifyToken()
    if (result.code == 200) {
        //获取车辆停车记录
        if (vehicle_num) {
            await parkingRecordModel.userlist(vehicle_num).then(
                (content) => {
                    res.send(content)
                },
                (error) => {
                    res.send(error)
                }
            )
        }
        else {
            res.send({
                code: -1,
                msg: '缺少参数。'
            })
        }
    }
    else {
        // 不是登录状态
        res.send({
            code: -1,
            msg: '您还没有登录哦。'
        })
    }
})


// 删除停车记录
// URL: /api/parkingRecord/remove
// 参数：token ,parking_record_id
// 请求方式：POST
router.post('/remove', async (req, res) => {
    // 获取token
    let token = req.body.token || req.headers.token;
    // 验证token
    let result = new Jwt(token).verifyToken();
    // 登录状态
    if (result.code == 200) {
        // 接收前端的参数
        let {parking_record_id} = req.body;
        if(parking_record_id!= undefined) {
            await parkingRecordModel.remove (parking_record_id)
            .then(
                (content)=>{
                    res.send(content)
                },
                (error)=> {
                    res.send(error)
                }
            )

        }
        else {
            res.send({
                code: -1,
                msg: '删除停车记录，缺少参数'
            })
        }
       
    }
    else {
        res.send({
            code: -1,
            msg: '登录失效,请重新登录。'
        })
    }

})

module.exports = router;